package net.rock.controller;

import net.rock.pojo.User;
import net.rock.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.function.Function;

@RestController
@RequestMapping("/inter-service/user")
public class InnerAccountController {
    @Autowired
    private UserService userService;

    private <T> Function<T, User> processUser(Function<T, User> fun) {
        Function<User, User> processor = user -> {
            if (user != null) {
                user.setPassword(null);
            }
            return user;
        };
        return fun.andThen(processor);
    }

    @RequestMapping("/fetchBySessionId")
    public User fetchBySessionId(@RequestParam("sessionId") String sessionId) {
        return processUser(userService::fetchUserBySessionId).apply(sessionId);
    }

    @RequestMapping("/fetchById")
    public User fetchById(@RequestParam("id") Integer id) {
        return processUser(userService::fetchUserById).apply(id);
    }
}
